package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.Question;

/**
 * A data access object (DAO) providing persistence and search support for
 * Question entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fick.hibernate.model.Question
 * @author MyEclipse Persistence Tools
 */

public class QuestionDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(QuestionDAO.class);

	// property constants
	public static final String QUESTION_CONTENT = "questionContent";

	public static final String USER_NAME = "userName";

	public static final String USER_ADDRESS = "userAddress";

	public static final String USER_TEL = "userTel";

	public static final String ANSWER_CONTENT = "answerContent";

	public static final String ADMIN_NAME = "adminName";

	public static final String ANSWER_DATE = "answerDate";

	public void attachClean(Question instance) {
		log.debug("attaching clean Question instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(Question instance) {
		log.debug("attaching dirty Question instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Question instances");
		try {
			String queryString = "from Question";
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			return queryObject.list();

		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByExample(Question instance) {
		log.debug("finding Question instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.Question").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public Question findById(java.lang.Integer id) {
		log.debug("getting Question instance with id: " + id);
		try {
			Question instance = (Question) getSession().get("com.fick.hibernate.model.Question", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Question instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Question as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findPageByHql(Integer currPage, Integer pageSize) {
		log.debug("find page by hql");
		try {
			String queryString = "from Question order by questionId desc";
			int a1 = currPage;
			int a2 = pageSize;
			Query query = getSession().createQuery(queryString);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(pageSize);
			List li = query.list();
			return li;
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error("find page by hql is filse", e);
		} finally {
			closeSession();
		}
		return null;
	}

	public List findPageByHql2(String hql, Integer currPage, Integer pageSize) {
		log.debug("find page by hql");
		try {
			String queryString = "from Question where " + hql + " order by questionId desc";
			int a1 = currPage;
			int a2 = pageSize;
			Query query = getSession().createQuery(queryString);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(pageSize);
			List li = query.list();
			return li;
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error("find page by hql is filse", e);
		}
		return null;
	}

	public Question merge(Question detachedInstance) {
		log.debug("merging Question instance");
		try {
			Question result = (Question) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}